#pragma once
#include <cstdio>
#include <vector>
#include <string>

class Command
{
private:
    bool safe(const std::string &cmd)
    {
        for (auto &e : _white_list)
        {
            if (e == cmd)
                return true;
        }
        return false;
    }

public:
    Command()
    {
        _white_list.emplace_back("ls -al");
        _white_list.emplace_back("ls");
        _white_list.emplace_back("whoami");
        _white_list.emplace_back("pwd");
    }
    std::string Excute(const std::string &cmd)
    {
        if (!safe(cmd))
            return "非法操作";
        FILE *fp = popen(cmd.c_str(), "r");
        if (nullptr == fp)
        {
            return "你要执行的命令不存在";
        }
        std::string data;
        char buff[512];
        while (fgets(buff, sizeof(buff), fp))
        {
            data += buff;
        }
        pclose(fp);
        return data;
    }

    ~Command()
    {
    }

private:
    std::vector<std::string> _white_list;
};